package wwk.com.android_meilishuo.ui.assort;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wwk.core.decoration.DividerGridItemDecoration;
import com.wwk.core.decoration.SpacesItemDecoration;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.List;

import wwk.com.android_meilishuo.R;
import wwk.com.android_meilishuo.mvp.presenter.AssortPresenter;
import wwk.com.android_meilishuo.mvp.view.MLSView;
import wwk.com.android_meilishuo.utils.GlideImageLoader;

/**
 * Created by Administrator on 2016/12/26.
 */

public class FragmentAssort extends Fragment implements MLSView.IAssortView {
    private Context mContext;
    private LinearLayout linear_layout_assort;
    private AssortPresenter mAssortPresenter;
    private Banner banner_assort;
    private RecyclerView recyclerView_AllAssort, recyclerView_Tag,
            recyclerView_Category1,recyclerView_Category2,recyclerView_Category3;
    private ImageView imageView_Assort_All, imageView_Assort_Tag,
            imageView_Assort_Category1,imageView_Assort_Category2,imageView_Assort_Category3;
    private Handler mHandler;

    public static FragmentAssort newInstance() {
        Bundle args = new Bundle();
        FragmentAssort fragment = new FragmentAssort();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mAssortPresenter = new AssortPresenter(this);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_assort, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        linear_layout_assort = (LinearLayout) view.findViewById(R.id.linear_layout_sort);
        //轮播布局
        banner_assort = (Banner) view.findViewById(R.id.banner_Assort);
        //全部分类
        imageView_Assort_All = (ImageView) view.findViewById(R.id.imageView_Assort_All);
        //热门标签
        imageView_Assort_Tag = (ImageView) view.findViewById(R.id.imageView_Assort_Tag);
        imageView_Assort_Category1 = (ImageView) view.findViewById(R.id.imageView_Assort_Category1);
        imageView_Assort_Category2 = (ImageView) view.findViewById(R.id.imageView_Assort_Category2);
        imageView_Assort_Category3 = (ImageView) view.findViewById(R.id.imageView_Assort_Category3);
        //分类布局
        recyclerView_AllAssort = (RecyclerView) view.findViewById(R.id.recyclerView_Assort);
        recyclerView_AllAssort.setHasFixedSize(true);
        GridLayoutManager gridManager_All = new GridLayoutManager(mContext, 4);
        recyclerView_AllAssort.setLayoutManager(gridManager_All);
        //热门标签
        recyclerView_Tag = (RecyclerView) view.findViewById(R.id.recyclerView_Tag);
        recyclerView_Tag.setHasFixedSize(true);
        recyclerView_Tag.addItemDecoration(new SpacesItemDecoration(10));
        GridLayoutManager gridManager_Tag = new GridLayoutManager(mContext, 3);
        recyclerView_Tag.setLayoutManager(gridManager_Tag);

        //热门穿搭
        recyclerView_Category1 = (RecyclerView) view.findViewById(R.id.recyclerView_Category1);
        recyclerView_Category1.setHasFixedSize(true);
        recyclerView_Category1.setNestedScrollingEnabled(false);
        recyclerView_Category1.addItemDecoration(new SpacesItemDecoration(15));
        LinearLayoutManager layoutManager_Category1 = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        recyclerView_Category1.setLayoutManager(layoutManager_Category1);

        //畅销热榜
        recyclerView_Category2 = (RecyclerView) view.findViewById(R.id.recyclerView_Category2);
        recyclerView_Category2.setHasFixedSize(true);
        recyclerView_Category2.setNestedScrollingEnabled(false);
        recyclerView_Category2.addItemDecoration(new SpacesItemDecoration(15));
        LinearLayoutManager layoutManager_Category2 = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        recyclerView_Category2.setLayoutManager(layoutManager_Category2);

        //猜你喜欢
        recyclerView_Category3 = (RecyclerView) view.findViewById(R.id.recyclerView_Category3);
        recyclerView_Category3.setHasFixedSize(true);
        recyclerView_Category3.setNestedScrollingEnabled(false);
        recyclerView_Category3.addItemDecoration(new SpacesItemDecoration(15));
        LinearLayoutManager layoutManager_Category3 = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        recyclerView_Category3.setLayoutManager(layoutManager_Category3);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAssortPresenter.loadPAssortData(mContext, imageView_Assort_All, imageView_Assort_Tag
        ,imageView_Assort_Category1,imageView_Assort_Category2,imageView_Assort_Category3);
    }

    @Override
    public Handler createBanner(final List<String> list_Url) {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 0:
                        banner_assort.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                        banner_assort.setImageLoader(new GlideImageLoader());
                        banner_assort.setImages(list_Url);
                        banner_assort.isAutoPlay(true);
                        banner_assort.setDelayTime(2500);
                        banner_assort.setIndicatorGravity(BannerConfig.CENTER);
                        banner_assort.start();
                        break;
                }
            }
        };
        return mHandler;
    }

    @Override
    public void toSetAllAdapter(AllAssortRecyclerAdapter adapter) {
        recyclerView_AllAssort.setAdapter(adapter);
    }

    @Override
    public void toSetTagAdapter(TagAssortRecyclerAdapter adapter) {
        recyclerView_Tag.setAdapter(adapter);
    }

    @Override
    public void toSetCategory1Adapter(Category1AssortRecyclerAdapter adapter) {
        recyclerView_Category1.setAdapter(adapter);
    }

    @Override
    public void toSetCategory2Adapter(Category2AssortRecyclerAdapter adapter) {
        recyclerView_Category2.setAdapter(adapter);
    }

    @Override
    public void toSetCategory3Adapter(Category3AssortRecyclerAdapter adapter) {
        recyclerView_Category2.setAdapter(adapter);
    }

}
